﻿

namespace Mangosoft.DiceCopy.DistributedServices.MainBoundedContext.InstanceProviders {

    using Mangosoft.DiceCopy.Application.MainBoundedContext.JobHuntingModule.Services;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.JobseekerAgg;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.NewsAgg;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.Adapter;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.Logging;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.NetFramework.Adapter;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.NetFramework.Logging;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.NetFramework.Validator;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.Validator;
    using Mangosoft.DiceCopy.Infrastructure.Data.MainBoundedContext.Repositories;
    using Mangosoft.DiceCopy.Infrastructure.Data.MainBoundedContext.UnitOfWork;
    using Microsoft.Practices.Unity;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.AdvertisingAgg;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.ResumeAgg;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.CoverLetterAgg;

    public static class Container {
        #region Properties

        static IUnityContainer _currentContainer;

        /// <summary>
        /// Get the current configured container
        /// </summary>
        /// <returns>Configured container</returns>
        public static IUnityContainer Current {
            get {
                return _currentContainer;
            }
        }

        #endregion

        #region Ctors
        static Container() {
            ConfigureContainer();

            ConfigureFactories();
        }
        #endregion

        #region Methods
        static void ConfigureContainer() {
            _currentContainer = new UnityContainer();

            // -> Unit of Work and Repositories
            _currentContainer.RegisterType(typeof(MainBCUnitOfWork), new PerResolveLifetimeManager());

            _currentContainer.RegisterType<IJobseekerRepository, JobseekerRepository>();
            _currentContainer.RegisterType<INewsRepository, NewsRepository>();
            _currentContainer.RegisterType<IAdvertisingRepository, AdvertisingRepository>();
            _currentContainer.RegisterType<IResumeRepository, ResumeRepository>();
            _currentContainer.RegisterType<ICoverLetterRepository, CoverLetterRepository>();




            // -> Adapters
            _currentContainer.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());

            // -> Domain Services

            // ->Application Services
            _currentContainer.RegisterType<IJobseekerAppService, JobseekerAppService>();
            _currentContainer.RegisterType<IJobHuntingAppService, JobHuntingAppService>();
            _currentContainer.RegisterType<IResumeAppService, ResumeAppService>();

            // ->Distributed Services
            _currentContainer.RegisterType<IJobHuntingModuleService, JobHuntingModuleService>();
        }

        static void ConfigureFactories() {
            LoggerFactory.SetCurrent(new TraceSourceLogFactory());
            EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());

            var typeAdapterFactory = _currentContainer.Resolve<ITypeAdapterFactory>();
            TypeAdapterFactory.SetCurrent(typeAdapterFactory);
        }

        #endregion
    }
}